package controllers;

import beans.InhoudBean1;
import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
import models.User;
import models.Course;
import tools.Checker;
import tools.Encryptor;

public class ChangeCourseController extends HttpServlet {

    private Dbmanager dbManager;
    private QueryManager queryManager;
    private String address;
    HttpSession sessie;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        sessie = request.getSession();
        User user = new User();
        Course course = new Course();

        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);

        if (request.getParameter("courseid") != null) {
            //Als er een id is meegegeven, worden de gegevens van de gebruiker opgehaald.
            int id = (int) Long.parseLong(request.getParameter("courseid"));

            course = queryManager.getCoursbyid(id);
        }

        if (sessie.getAttribute("changeCourse") != null) {
            course = (Course) sessie.getAttribute("changeCourse");
        }

        user = (User) sessie.getAttribute("gebruikerinfo");

        request.setAttribute("userlevel", user.getLevel());
        request.setAttribute("course", course);
        request.setAttribute("foutMelding", sessie.getAttribute("foutMelding"));

        String address = "/change_course.jsp";
        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response); //Stuurt door naar de Gebruikers index

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        sessie = request.getSession();
        Course course = new Course();
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);
        String coursename = "";
        String coursedescription = "";
        String courselanguage = "";
        double courseprice = 0.0;
        int ID = 0;
        boolean isEmpty = false;
        InhoudBean1 inhoud = (InhoudBean1) sessie.getAttribute("inhoud");


        if (!request.getParameter("coursename").isEmpty()) {
            coursename = request.getParameter("coursename");
        } else {
            isEmpty = true;
            sessie.setAttribute("foutMelding", inhoud.getError1());
        }

        if (!request.getParameter("description").isEmpty()) {
            coursedescription = request.getParameter("description");
        } else {
            isEmpty = true;
            sessie.setAttribute("foutMelding", inhoud.getError2());
        }

        if (!request.getParameter("courselanguage").isEmpty()) {
            courselanguage = request.getParameter("courselanguage");
        } else {
            isEmpty = true;
            sessie.setAttribute("foutMelding", inhoud.getError3());
        }
        try {
            if (!request.getParameter("price").isEmpty()) {
                Checker checker = new Checker();
                String priceS = request.getParameter("price"), priceS2 = "";
                for (int i = 0; i < priceS.length(); i++) {
                    char temp;
                    temp = priceS.charAt(i);
                    if (temp == ',') {
                        temp = '.';
                    }
                    priceS2 = priceS2 + temp;
                }
                if (checker.isDigit(priceS2)) {
                    courseprice = Double.parseDouble(priceS2);
                    course.setPrice(courseprice);
                } else {
                    sessie.setAttribute("foutMelding", inhoud.getError5());
                    isEmpty = true;
                }
            } else {
                isEmpty = true;
                sessie.setAttribute("foutMelding", inhoud.getError4());
            }
        } catch (Exception ex) {
        }

        if (request.getParameter("ID") != null) {
            ID = Integer.parseInt(request.getParameter("ID"));
        }

        course.setCourseID(ID);
        course.setCourseDescription(coursedescription);
        course.setCourseLanguage(courselanguage);
        course.setCourseName(coursename);
        

        if (!isEmpty) {
            queryManager.updateCourse(course);
            sessie.setAttribute("foutMelding", null);
            dbManager.closeConnection();
            response.sendRedirect("view_editable_courses");
        } else {
            sessie.setAttribute("changeCourse", course);
            dbManager.closeConnection();
            response.sendRedirect("change_course?courseid=" + ID);
        }

        
    }
}
